<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="日期" prop="day">
          <el-date-picker clearable
            v-model="queryParams.day"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['index:postcard:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['index:postcard:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['index:postcard:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['index:postcard:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="postcardList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="日期" align="center" prop="day" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.day, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center" prop="picture">
          <template v-slot="{ row }">
            <div
              class="a_b"
              v-if="row.picture && row.picture != ''"
              style="height: 100px; margin: 0 auto; width: 100px"
            >
              <img :src="baseUrl + row.picture" alt="" class="a_b_a" />
              <span class="a_b_b" style="text-align: center">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(row.picture)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['index:postcard:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['index:postcard:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改明信片对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="postcardRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="日期" prop="day">
            <el-date-picker clearable
              v-model="form.day"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="图片" prop="picture">
            <div class="a">
                <div class="a_b" v-if="form.picture && form.picture != ''">
                <img :src="baseUrl + form.picture" alt="" class="a_b_a" />
                <span class="a_b_b">
                    <span
                    class="a_b_b_a"
                    @click="handlePictureCardPreview(form.picture)"
                    >
                    <el-icon><zoom-in /></el-icon>
                    </span>
                    <span
                    class="a_b_b_b"
                    @click="handleDeletePicture(form.picture, 4)"
                    >
                    <el-icon>
                        <Delete />
                    </el-icon>
                    </span>
                </span>
                </div>
                <div class="a_a" @click="uploadClick(4)" v-else>
                <el-icon>
                    <Plus />
                </el-icon>
                <input
                    ref="inputRef4"
                    name="file"
                    accept=".jpg,.jpeg,.png"
                    type="file"
                    style="display: none"
                    @change="(event) => changeFile(event, 4)"
                />
                </div>
            </div>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="Postcard">
  import { listPostcard, getPostcard, delPostcard, addPostcard, updatePostcard } from "@/api/index/postcard";
  import { uploadFile } from "@/api/imgupload/index";
  
  const { proxy } = getCurrentInstance();
  
  const postcardList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      day: null,
      picture: null,
      text: null,
      author: null,
      valiFlag: null
    },
    rules: {
      day: [
        { required: true, message: "日期不能为空", trigger: "blur" }
      ],
      picture: [
        { required: true, message: "图片不能为空", trigger: "blur" }
      ],
      // text: [
      //   { required: true, message: "文本不能为空", trigger: "blur" }
      // ],
      // author: [
      //   { required: true, message: "作者不能为空", trigger: "blur" }
      // ],
      createTime: [
        { required: true, message: "创建时间不能为空", trigger: "blur" }
      ],
      createBy: [
        { required: true, message: "创建人不能为空", trigger: "blur" }
      ],
      updateTime: [
        { required: true, message: "修改时间不能为空", trigger: "blur" }
      ],
      updateBy: [
        { required: true, message: "修改人不能为空", trigger: "blur" }
      ],
      valiFlag: [
        { required: true, message: "有效标志1有效0无效(无效数据任何地方不可见)不能为空", trigger: "blur" }
      ]
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询明信片列表 */
  function getList() {
    loading.value = true;
    listPostcard(queryParams.value).then(response => {
      postcardList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      id: null,
      day: null,
      picture: null,
      text: null,
      author: null,
      createTime: null,
      createBy: null,
      updateTime: null,
      updateBy: null,
      valiFlag: null
    };
    proxy.resetForm("postcardRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加明信片";
  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getPostcard(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改明信片";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["postcardRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updatePostcard(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addPostcard(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除明信片编号为"' + _ids + '"的数据项？').then(function() {
      return delPostcard(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('index/postcard/export', {
      ...queryParams.value
    }, `postcard_${new Date().getTime()}.xlsx`)
  }
  
  /** 图片上传 */
  //图片上传
  const inputRef4 = ref();
  const dialogImageUrl = ref();
  const dialogVisible = ref(false);
  const baseUrl = import.meta.env.VITE_BASE_URL;
  const uploadClick = (type) => {
    proxy.$refs[`inputRef${type}`].click();
  };
  const changeFile = async (e, type) => {
    let files = e.target.files;
    for (let i = 0; i < files.length; i++) {
      let _file = files[i];
      var file = new FormData();
      file.append("files", _file);
      let res = await uploadFile(file);
      if (res.code == 200) {
        if (type == 4) {
          form.value.picture = res.fileNames;
        }
      }
    }
  };
  const handlePictureCardPreview = (file) => {
    dialogImageUrl.value = baseUrl + file;
    dialogVisible.value = true;
  };
  const handleDeletePicture = (file, type) => {
    if (type == 4) {
        form.value.picture = "";
    }
  };
  getList();
  </script>
  
  <style scoped lang="scss">
.tabclass {
  width: 100%;
  ::v-deep {
    .el-form-item__error {
      display: contents;
    }
  }
}
.content {
  position: relative;

  &::-webkit-scrollbar {
    width: 2px;
    /* 滚动条宽度 */
  }

  /* 自定义滚动条轨道 */
  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    /* 轨道颜色 */
  }

  /* 自定义滚动条的滑块（thumb） */
  &::-webkit-scrollbar-thumb {
    background: #888;
    /* 滑块颜色 */
  }

  /* 当hover或active状态时，自定义滑块的颜色 */
  &::-webkit-scrollbar-thumb:hover {
    background: #555;
  }
}

.content_back {
  position: relative;

  &::after {
    content: "";
    position: absolute;
    // width: 100%;
    // height: 100%;
    // background: rgba(0,0,0,0.1);
    top: -20px;
    left: -20px;
    right: -20px;
    bottom: -20px;
    z-index: 100;
  }
}

.a {
  display: flex;
  flex-wrap: wrap;
}

.a_a {
  width: 104px;
  height: 104px;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    border-color: var(--el-color-primary);
  }
}

.a_b {
  margin: 0 8px 8px 0;
  border-radius: 6px;
  width: 104px;
  height: 104px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--el-border-color);

  .a_b_a {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .a_b_b {
    align-items: center;
    background-color: var(--el-overlay-color-lighter);
    color: #fff;
    cursor: default;
    display: inline-flex;
    font-size: 20px;
    height: 100%;
    justify-content: center;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    transition: opacity var(--el-transition-duration);
    width: 100%;

    .a_b_b_a {
      cursor: pointer;
      display: none;
    }

    .a_b_b_b {
      color: inherit;
      font-size: inherit;
      position: static;
      cursor: pointer;
      display: none;
      margin-left: 1rem;
    }
  }
}

.a_b:hover {
  .a_b_b {
    opacity: 1;
  }

  .a_b_b_a {
    display: block;
  }

  .a_b_b_b {
    display: block;
  }
}

.a_subTitle {
  font-size: 12px;
  color: red;
  line-height: 1.5;
  margin-top: 4px;
  width: 100%;
}

.b_subTitle {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  line-height: 1.7;
  margin-top: 4px;
  width: 100%;

  p {
    margin: 0;
    padding: 0;
  }
}

.c {
  display: flex;
  align-items: center;
  margin: 4px 0;
  width: 100%;

  .c_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }

  .c_b {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}

.append ::v-deep(.el-input-group__append) {
  padding: 0 10px;
}

.d {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;

  .d_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}
</style>
